<?php

namespace App\Http\Controllers\CmsApi;

use App\Lib\Response;
use Illuminate\Http\Request;
use App\Exceptions\ErrorCodeException;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\Gift;
use App\Http\Services\ColorGiftServices;
use App\Http\Services\GiftServices;

class InnerGiftController extends BaseController
{
    protected $gift_model;
    protected $giftServices;
    protected $colorGiftServices;

    public function __construct()
    {
        $this->gift_model = new Gift();
        $this->giftServices = new GiftServices();
        $this->colorGiftServices = new ColorGiftServices();

    }

    /**
     * 新增礼物
     */
    public function addGift(Request $request)
    {
        $param['name'] = (string) $request->input("name");//礼物名称
        $param['gift_type'] = (int) $request->input("gift_type");//类型 1=普通礼物 2=幸运礼物 3=盲盒礼物 4=富豪值礼物 5=彩蛋礼物 6=套盒礼物 7=新幸运礼物 8=增值服务 9=礼物抵扣券 10=热度值礼物
        $param['price'] = (int) $request->input("price");
        $param['image'] = (string) $request->input("image");
        $param['animate_resource'] = getUrlPath($request->input("animate_resource"));
        $param['animate_resource_md5'] = getUrlPath($request->input("animate_resource_md5"));
        $param['is_show'] = (int) $request->input("is_show");// 1:是 0:否
        $param['gift_sort'] = (int) $request->input("gift_sort");
        $param['wealth_num'] = (int) $request->input("wealth_num");//自定义富豪值
        $param['hot_num'] = (int) $request->input("hot_num");//自定义热度值
        $param['ext_data'] = (string) $request->input("ext_data");//扩展字段
        if(empty($param['name'])) {
            Response::returnResult(Hint::PARAM_ERROR, [], '礼物名称不能为空');
        }
        if(empty($param['gift_type'])) {
            Response::returnResult(Hint::PARAM_ERROR, [], '礼物类型不能为空');
        }

        $data = $this->giftServices->addGift($param);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 设置套盒礼物奖池
     */
    public function setSetboxGiftPool(Request $request)
    {
        $giftId = (int) $request->input("gift_id");
        $extData = (string) $request->input("ext_data");//扩展字段
        $extData = json_decode($extData, true);
        if(empty($giftId) || empty($extData)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->giftServices->setSetboxGiftPool($giftId, $extData);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 设置冠名礼物
     *
     * @param Request $request
     * @throws ErrorCodeException
     */
    public function setGiftNaming(Request $request)
    {
        $rules = [
            'user_id' => 'required',
            'gift_id' => 'required',
        ];
        $messages = [
            'user_id' => '请输入用户ID',
            'gift_id' => '请输入礼物ID',
        ];
        $params = parent::validateParams($request->all(), $rules, $messages);
        $this->gift_model->setGiftNaming($params['user_id'], $params['gift_id'], 7);
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 设置幸运礼物奖池
     *
     * @param Request $request
     * @throws ErrorCodeException
     */
    public function setLuckGiftPoll(Request $request)
    {
        $giftId = $request->input("gift_id");
        $poolList = $request->input("pool_list");
        if (empty($giftId)) {
            Response::returnResult(Hint::PARAM_ERROR, [], '礼物参数错误');
        }

        $poolList = json_decode($poolList, true);
        if (empty($poolList) || !is_array($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR, [], '奖池参数错误');
        }
        $data = $this->giftServices->setLuckGiftPoll($giftId, $poolList);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    //设置彩蛋礼物奖池
    public function setColorGiftPool(Request $request){
        $giftId = $request->post("gift_id");
        if (empty($giftId)) {
            Response::returnResult(Hint::PARAM_ERROR, [], '礼物参数错误');
        }
        $name = $request->post('name');
        if (empty($name)) {
            Response::returnResult(Hint::PARAM_ERROR, [], '参数错误,缺少奖池名称');
        }
        $rate = $request->post('rate',0);//概率
        $min_count = $request->post('min_count',0);//最小抽
        $max_count = $request->post('max_count',0);//最大抽
        $hours = $request->post('hours',0);//间隔时间
        $data = $this->colorGiftServices->colorGiftAddPool($giftId, $name, $rate, $min_count, $max_count, $hours);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 彩蛋礼物编辑奖池
     */
    public function colorGiftEditPool(Request $request)
    {
        $poolId = $request->input('pool_id', 0);
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_count', 0);
        $maxNum = (int) $request->input('max_count', 0);
        $rate = (int) $request->input('rate', 0);
        $hours = (int) $request->input('hours', 0);
        if(empty($poolId) || empty($poolName) || empty($minNum) || empty($maxNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('COLORGIFT:colorGiftEditPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->colorGiftServices->colorGiftEditPool($poolId, $poolName, $minNum, $maxNum, $rate, $hours);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新增彩蛋概率配置
     */
    public function addPoolRate(Request $request){
        $poolId = (int) $request->input('pool_id', 0);
        $poolList = $request->input('pool_list');
        if(empty($poolId) || empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $poolList = json_decode($poolList, true);
        if(empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('COLORGIFT:colorGiftAddPoolConfig', 2);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }
        $data = $this->colorGiftServices->addPoolRate($poolId, $poolList);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    //删除概率配置
    public function colorGiftRateDel(Request $request){
        $poolId = (int) $request->input('pool_id', 0);
        $rate_num = (int) $request->input('rate_num', 0);
        if(empty($poolId) || empty($rate_num)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->colorGiftServices->colorGiftRateDel($poolId, $rate_num);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 彩蛋礼物奖池保存奖品配置
     */
    public function colorGiftSavePoolConfig(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolList = $request->input('pool_list');
        if(empty($poolId) || empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $poolList = json_decode($poolList, true);
        if(empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('COLORGIFT:colorGiftSavePoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->colorGiftServices->colorGiftSavePoolConfig($poolId, $poolList);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 彩蛋池开启/关闭
     */
    public function colorGiftSetPoolStatus(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolStatus = (int) $request->input('pool_status', 0);
        if(empty($poolId) || empty($poolStatus)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'COLORGIFT:colorGiftPool';
        $ifLock = redis_lock($key, 3600);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '彩蛋更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->colorGiftServices->colorGiftSetPoolStatus($poolId, $poolStatus);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 彩蛋更新奖池缓存
     */
    public function colorGiftPoolReset(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'COLORGIFT:colorGiftPool';
        $ifLock = redis_lock($key, 3600);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '彩蛋更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->colorGiftServices->colorGiftPoolReset($poolId);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 彩蛋奖池配置剩余数量
     */
    public function colorGiftPoolConfigNum(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $rate_num = (int) $request->input('rate_num',0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->colorGiftServices->colorGiftPoolConfigNum($poolId,$rate_num);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    //彩蛋礼物配置上限配置
    public function setColorGiftMaxSet(Request $request){
        $maxOut = $request->post('max_out',0);
        $maxIn = $request->post('max_in',0);
        $data = $this->giftServices->setColorGiftMax($maxOut, $maxIn);
        Response::returnResult(Hint::SUCCESS,$data);
    }

    //彩蛋下池子概率配置banner
    public function colorGiftRateAddBanner(Request $request){
        $data = $this->colorGiftServices->colorGiftRateAddBanner($request->all());
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    //新增彩蛋抽奖白名单
    public function colorGiftWhiteAdd(Request $request){
        $data = $this->colorGiftServices->colorGiftWhiteAdd($request->all());
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    //编辑彩蛋白名单
    public function colorWhiteEdit(Request $request){
        $data = $this->colorGiftServices->colorWhiteEdit($request->all());
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    //删除彩蛋白名单配置
    public function colorWhiteDel(Request $request){
        $data = $this->colorGiftServices->colorWhiteDel($request->all());
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物新增奖池
     */
    public function luckGiftAddPool(Request $request)
    {
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $giftPrice = (int) $request->input('gift_price', 0);
        if(empty($poolName) || empty($minNum) || empty($maxNum) || empty($giftPrice)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('LUCKGIFT:luckGiftAddPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->giftServices->luckGiftAddPool($poolName, $minNum, $maxNum, $giftPrice);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物编辑奖池
     */
    public function luckGiftEditPool(Request $request)
    {
        $poolId = $request->input('pool_id', 0);
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $giftPrice = (int) $request->input('gift_price', 0);
        if(empty($poolId) || empty($poolName) || empty($minNum) || empty($maxNum) || empty($giftPrice)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('LUCKGIFT:luckGiftEditPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->giftServices->luckGiftEditPool($poolId, $poolName, $minNum, $maxNum, $giftPrice);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物新增奖池配置
     */
    public function luckGiftAddPoolConfig(Request $request)
    {
        $poolId = $request->input('pool_id', 0);
        $diamondNum = (int) $request->input('diamond_num', 0);
        $dataNum = (int) $request->input('data_num', 0);
        if(empty($poolId) || empty($dataNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('LUCKGIFT:luckGiftAddPoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->giftServices->luckGiftAddPoolConfig($poolId, $diamondNum, $dataNum);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物编辑奖池配置
     */
    public function luckGiftEditPoolConfig(Request $request)
    {
        $id = $request->input('id', 0);
        $diamondNum = (int) $request->input('diamond_num', 0);
        $dataNum = (int) $request->input('data_num', 0);
        if(empty($id) || empty($dataNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('LUCKGIFT:luckGiftEditPoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->giftServices->luckGiftEditPoolConfig($id, $diamondNum, $dataNum);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物删除奖池配置
     */
    public function luckGiftDelPoolConfig(Request $request)
    {
        $id = $request->input('id', 0);
        if(empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('LUCKGIFT:luckGiftDelPoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->giftServices->luckGiftDelPoolConfig($id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物更新奖池缓存
     */
    public function luckGiftPoolReset(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'LUCKGIFT:luckGiftPool';
        $ifLock = redis_lock($key, 3600);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '新幸运礼物更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->giftServices->luckGiftPoolReset($poolId);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物设置奖池开启/关闭
     */
    public function luckGiftSetPoolStatus(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolStatus = (int) $request->input('pool_status', 0);
        if(empty($poolId) || empty($poolStatus)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'LUCKGIFT:luckGiftPool';
        $ifLock = redis_lock($key, 3600);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '新幸运礼物更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->giftServices->luckGiftSetPoolStatus($poolId, $poolStatus);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物奖池配置剩余数量
     */
    public function luckGiftPoolConfigNum(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->giftServices->luckGiftPoolConfigNum($poolId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物新增白名单
     */
    public function luckGiftAddWhite(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $diamondNum = $request->input('diamond_num', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($userId) || empty($diamondNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('LUCKGIFT:luckGiftAddWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->giftServices->luckGiftAddWhite($userId, $diamondNum, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新幸运礼物删除白名单
     */
    public function luckGiftDelWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('LUCKGIFT:luckGiftDelWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->giftServices->luckGiftDelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    //背包礼物来源类型
    public function bagGiftPlatformType(Request $request){
        $data = $this->giftServices->bagGiftPlatformType();
        Response::returnResult(Hint::SUCCESS,$data);
    }





}